package org.googlecode.jwf.demo;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ConnectorTypePanel extends JPanel {

    private boolean agreementChecked_ = false;

    private ButtonGroup connectorTypeGroup_;

    public ConnectorTypePanel() {
        setupUI();
    }

    private void setupUI() {
        setLayout(new BorderLayout());

        add(new JLabel("Please enter your favorite type of data connector:"),
                BorderLayout.NORTH);

        // set up connector type choose panel
        JPanel connTypeChoosePanel = new JPanel(new GridLayout(0, 1));

        connectorTypeGroup_ = new ButtonGroup();

        JRadioButton wirelessRadioButton = new JRadioButton("802.11 b/g");
        JRadioButton ethernetRJRadioButton = new JRadioButton("Ethernet RJ-45");
        JRadioButton ethernetTenRadioButton = new JRadioButton(
                "Ethernet 10 base T");
        JRadioButton serialParallelRadioButton = new JRadioButton(
                "Serial/Parallel");
        JRadioButton notInventedYetRadioButton = new JRadioButton(
                "Something Not Yet Invented But You're Sure You'll Want It");

        ethernetRJRadioButton.setSelected(true);

        wirelessRadioButton.setActionCommand("Wireless Radio");
        ethernetRJRadioButton.setActionCommand("Ethernet RJ-45");
        ethernetTenRadioButton.setActionCommand("Ethernet 10 base T");
        serialParallelRadioButton.setActionCommand("Serial/Parallel");
        notInventedYetRadioButton.setActionCommand("Not Yet Invented");

        connectorTypeGroup_.add(wirelessRadioButton);
        connectorTypeGroup_.add(ethernetRJRadioButton);
        connectorTypeGroup_.add(ethernetTenRadioButton);
        connectorTypeGroup_.add(serialParallelRadioButton);
        connectorTypeGroup_.add(notInventedYetRadioButton);

        connTypeChoosePanel.add(new JLabel());
        connTypeChoosePanel.add(wirelessRadioButton);
        connTypeChoosePanel.add(ethernetRJRadioButton);
        connTypeChoosePanel.add(ethernetTenRadioButton);
        connTypeChoosePanel.add(serialParallelRadioButton);
        connTypeChoosePanel.add(notInventedYetRadioButton);
        connTypeChoosePanel.add(new JLabel());

        JCheckBox agreeCheckBox = new JCheckBox(
                "I agree to laugh at people who chose options other than mine");
        agreeCheckBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                agreeCheckBoxStateChanged((JCheckBox) e.getSource());
            }
        });
        connTypeChoosePanel.add(agreeCheckBox);
        connTypeChoosePanel.add(new JLabel());

        add(connTypeChoosePanel, BorderLayout.CENTER);
    }

    public String getSelectedConnectorType() {
        return connectorTypeGroup_.getSelection().getActionCommand();
    }

    public boolean getAgreementChecked() {
        return agreementChecked_;
    }

    private void agreeCheckBoxStateChanged(JCheckBox agreeCheckBox) {
        boolean oldValue = agreementChecked_;
        agreementChecked_ = agreeCheckBox.isSelected();
        firePropertyChange("agreementChecked", oldValue, agreementChecked_);
    }
}
